package im.zego.zimexample.ui.peer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.Toast;

import androidx.annotation.Nullable;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

import im.zego.zim.ZIM;
import im.zego.zim.callback.ZIMMessageQueriedCallback;
import im.zego.zim.callback.ZIMMessageSentCallback;
import im.zego.zim.entity.ZIMCustomMessage;
import im.zego.zim.entity.ZIMError;
import im.zego.zim.entity.ZIMMessage;
import im.zego.zim.entity.ZIMTextMessage;
import im.zego.zim.enums.ZIMErrorCode;
import im.zego.zim.enums.ZIMMessagePriority;
import im.zego.zimexample.R;
import im.zego.zimexample.SDKManager;
import im.zego.zimexample.emus.ChatItemType;
import im.zego.zimexample.entity.MessageInfo;
import im.zego.zimexample.ui.base.ZIMSessionBashActivity;


public class ZIMPeerSessionActivity extends ZIMSessionBashActivity implements ZIMMessageSentCallback, SDKManager.OnReceivePeerMessage, ZIMMessageQueriedCallback {

    public static void actionStart(Activity mainActivity, String userID, String fromUserID, String userName) {
        Intent intent = new Intent(mainActivity, ZIMPeerSessionActivity.class);
        intent.putExtra("userID", userID);
        intent.putExtra("fromUserID", fromUserID);
        intent.putExtra("userName", userName);

        mainActivity.startActivity(intent);
    }

    String TAG = "ZIMPeerSessionActivity";
    private String userID, fromUserID, userName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        userID = getIntent().getStringExtra("userID");
        userName = getIntent().getStringExtra("userName");

        fromUserID = getIntent().getStringExtra("fromUserID");

        setTitle(userID);

        SDKManager.sharedInstance().addReceivePeerMessageCallback(this);
        SDKManager.sharedInstance().queryPeerMessage(userID, 0, 30, true, ZIMPeerSessionActivity.this);
    }

    public void sendMsg(String content) {
        if ("".equals(content.replace(" ", ""))) {
            Toast.makeText(ZIMPeerSessionActivity.this, "Please enter valid content", Toast.LENGTH_LONG).show();
            return;
        }

        CheckBox checkBox = findViewById(R.id.isBinary);
        ZIMMessage zimMessage;
        if (checkBox.isChecked()) {
            ZIMCustomMessage zimCustomMessage = new ZIMCustomMessage();
            zimCustomMessage.message = content.getBytes(StandardCharsets.UTF_8);
            zimCustomMessage.priority = ZIMMessagePriority.HIGH;
            zimMessage = zimCustomMessage;
        } else {
            ZIMTextMessage zimTextMessage = new ZIMTextMessage();
            zimTextMessage.message = content;
            zimTextMessage.priority = ZIMMessagePriority.HIGH;
            zimMessage = zimTextMessage;
        }
        SDKManager.sharedInstance().sendMsg(zimMessage, userID, this);

        MessageInfo messageInfo = new MessageInfo();
        messageInfo.setContent(checkBox.isChecked() ? "binary: " + content : content);
        messageInfo.setTime(System.currentTimeMillis());
        messageInfo.setType(ChatItemType.CHAT_ITEM_TYPE_RIGHT.value);
        messageInfo.setUserID(fromUserID);
        messageInfo.setUserName(userName);

        addMsg(messageInfo);
    }

    @Override
    protected void onLoadMsg() {
        super.onLoadMsg();
        MessageInfo info = getStartMessage();
        if (info != null) {
            SDKManager.sharedInstance().queryPeerMessage(userID, info.getMessageID(), 30, true, ZIMPeerSessionActivity.this);
        }
    }

    @Override
    public void onMessageSent(ZIMMessage zimMessage, ZIMError zimError) {
        if (zimError.code != ZIMErrorCode.SUCCESS) {
            Toast.makeText(this, "Failed to send message error: " + zimError.code.name(), Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        SDKManager.sharedInstance().showDebugLog(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        SDKManager.sharedInstance().closeDebugLog(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SDKManager.sharedInstance().removeReceivePeerMessageCallback(this);
    }


    @Override
    public void onReceivePeerMessage(ZIM zim, ArrayList<ZIMMessage> messageList, String fromUserID) {
        for (ZIMMessage message : messageList) {
            if (message instanceof ZIMTextMessage && message.userID.equals(userID)) {
                if (fromUserID.equals(message.userID)) {
                    MessageInfo messageInfo = new MessageInfo();
                    messageInfo.setContent(((ZIMTextMessage) message).message);
                    messageInfo.setMessageID(message.messageID);
                    messageInfo.setUserID(message.userID);
                    messageInfo.setTime(message.timestamp);
                    messageInfo.setType(ChatItemType.CHAT_ITEM_TYPE_LEFT.value);
                    addMsg(messageInfo);
                }
            }
            if (message instanceof ZIMCustomMessage && message.userID.equals(userID)) {
                if (fromUserID.equals(message.userID)) {
                    MessageInfo messageInfo = new MessageInfo();
                    messageInfo.setContent(new String(((ZIMCustomMessage) message).message, StandardCharsets.UTF_8));
                    messageInfo.setMessageID(message.messageID);
                    messageInfo.setUserID(message.userID);
                    messageInfo.setTime(message.timestamp);
                    messageInfo.setType(ChatItemType.CHAT_ITEM_TYPE_LEFT.value);
                    addMsg(messageInfo);
                }
            }

        }
    }

    @Override
    public void onMessageQueried(ArrayList<ZIMMessage> arrayList, ZIMError zimError) {
        for (ZIMMessage message : arrayList) {
            if (message instanceof ZIMTextMessage) {
                MessageInfo messageInfo = new MessageInfo();
                messageInfo.setContent(((ZIMTextMessage) message).message);
                messageInfo.setMessageID(message.messageID);
                messageInfo.setTime(message.timestamp);
                messageInfo.setUserID(message.userID);
                if (message.userID.equals(userID)) {
                    messageInfo.setType(ChatItemType.CHAT_ITEM_TYPE_LEFT.value);
                } else {
                    messageInfo.setType(ChatItemType.CHAT_ITEM_TYPE_RIGHT.value);
                }
                addMsg(messageInfo);
            }
            if (message instanceof ZIMCustomMessage) {
                MessageInfo messageInfo = new MessageInfo();
                messageInfo.setMessageID(message.messageID);
                messageInfo.setUserID(message.userID);
                messageInfo.setContent("binary: " + new String(((ZIMCustomMessage) message).message, StandardCharsets.UTF_8));

                messageInfo.setTime(message.timestamp);
                if (message.userID.equals(userID)) {
                    messageInfo.setType(ChatItemType.CHAT_ITEM_TYPE_LEFT.value);
                } else {
                    messageInfo.setType(ChatItemType.CHAT_ITEM_TYPE_RIGHT.value);
                }
                addMsg(messageInfo);

            }

        }
    }
}
